package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import com.icss.beans.StuClassVO;

/**
 * 班级管理
 * @author 梁海成
 *
 */
public class StuClassDao {
	
	Dao dao = new Dao();
	ResultSet rs = null;
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");

	/**
	 * 获取所有班级信息
	 * @return
	 */
	public ArrayList<StuClassVO> getAllClass() {

		String sql = "select * from StuClass";
		ArrayList<StuClassVO> classList = new ArrayList<StuClassVO>();
		rs= dao.query(sql);
		try {
			while (rs.next()) {
				StuClassVO c = new StuClassVO();	
				c.setClassName(rs.getString("className"));
				c.setStuClassId(rs.getInt("stuClassID"));
				c.setClassTime(sdf.format(rs.getDate("classTime")));
				classList.add(c);				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return classList;
	}
	
	/**
	 * 获取班级人数
	 * @param classID
	 * @return
	 */
	public int getClassCount(int classID){
		String sql ="select count(*) from Users where stuClassID = "+classID;
		Dao dao = new Dao();
		rs = dao.query(sql);
		int count=0;
		try {
			if(rs.next()){
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
		dao.close();
		return count;
	}
	 
	/**
	 * 添加班级
	 * @param s
	 */
	public  void addClass(StuClassVO s){
		String sql = "insert into StuClass (className,classTime) values('"+s.getClassName()+"','"+s.getClassTime()+"')";
		dao.execute(sql);
		close();
	}
	
	public StuClassVO getOneClass(int classID){
		String sql = "select * from StuClass where stuClassID="+classID;
		rs=dao.query(sql);
		StuClassVO scv = new StuClassVO();
		try {
			if(rs.next()){
				scv.setClassName(rs.getString("className"));
				scv.setClassTime(sdf.format(rs.getDate("classTime")));
				scv.setStuClassId(rs.getInt("stuClassID"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return scv;
	}
	
	/**
	 * 更新班级
	 * @param s
	 */
	public void updateClass(StuClassVO s){
		String sql = "update StuClass set className='"+s.getClassName()+"',classTime='"+s.getClassTime()+"' where stuClassID="+s.getStuClassId();
		dao.execute(sql);
		close();
	}
	
	/**
	 * 删除班级
	 * @param classID
	 */
	public void delClass(int classID){
		String sql = "delete from StuClass where stuClassID="+classID;
		dao.execute(sql);
		close();
	}
	
	/**
	 * 批量删除班级
	 * @param str
	 */
	public void delAllSelect(String str){
		String[] arr = str.split(",");
		
		for(int i=0;i<arr.length;i++){
			String sql = "delete from StuClass where stuClassID="+Integer.parseInt(arr[i]);			
			dao.execute(sql);
		}
		close();
	}
	
	/**
	 *  关闭连接 
	 *
	 */
	public void close(){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dao.close();
			
		}
	}
}
